package com.smec.remes.module.scan.activity;

import android.app.ProgressDialog;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.smec.moblie.R;
import com.smec.moblie.databinding.ActivityAllocBinding;
import com.smec.remes.base.SmecBaseActivity;
import com.smec.remes.module.scan.SmecScanPresenter;
import com.smec.remes.module.scan.adapter.SmecAllocAdapter;
import com.smec.remes.module.scan.beans.SmecAllocOrder;
import com.smec.remes.util.TopBarLayout;

import static com.smec.remes.module.scan.SmecScanPresenter.GET_ALLOC_FAILURE;
import static com.smec.remes.module.scan.SmecScanPresenter.GET_ALLOC_SUCCESS;

/**
 * Created by xupeizuo on 2018/3/28.
 */

public class SmecAllocActivity extends SmecBaseActivity<SmecScanPresenter> {

    private SmecAllocOrder smecAllocOrder;
    private ActivityAllocBinding activityAllocBinding;
    private ProgressDialog progressDialog=null;
    private SmecAllocAdapter smecAllocAdapter;

    @Override
    public SmecScanPresenter getPresenter() {
        return new SmecScanPresenter(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        smecAllocOrder=new SmecAllocOrder();
        activityAllocBinding= DataBindingUtil.setContentView(this, R.layout.activity_alloc);
        activityAllocBinding.setAlloc(smecAllocOrder);
        getDataFromIntent();
        initView();
    }

    public void getDataFromIntent(){
       final String no=getIntent().getStringExtra("allocOrderNo");
        activityAllocBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
               if(progressDialog == null){
                   progressDialog=new ProgressDialog(SmecAllocActivity.this);
                   progressDialog.setMessage("获取调拨单信息");
                   progressDialog.setCanceledOnTouchOutside(false);
               }
                progressDialog.show();
                mPresenter.getAllocMsg(no);
            }
        },200);
    }

    private void initView(){
        activityAllocBinding.searchTopbar.setTopBarListener(new TopBarLayout.TopBarListener() {
            @Override
            public void setOnLeftClickListener() {
                finish();
            }

            @Override
            public void setOnRight1ClickListener() {

            }

            @Override
            public void setOnRight2ClickListener() {

            }
        });
        smecAllocAdapter=new SmecAllocAdapter(this);
        activityAllocBinding.myRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        activityAllocBinding.myRecyclerView.setAdapter(smecAllocAdapter);
        activityAllocBinding.smecAlloc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(activityAllocBinding.smecDetails.getVisibility() == View.VISIBLE){
                    activityAllocBinding.smecAlloc.setText("点击展开");
                    activityAllocBinding.smecDetails.setVisibility(View.GONE);
                }else {
                    activityAllocBinding.smecAlloc.setText("点击收起");
                    activityAllocBinding.smecDetails.setVisibility(View.VISIBLE);
                }

            }
        });
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_ALLOC_SUCCESS)
            }
    )
    public void getAllocSuccess(SmecAllocOrder s){
        if(progressDialog != null && progressDialog.isShowing()){
            activityAllocBinding.getRoot().postDelayed(new Runnable() {
                @Override
                public void run() {
                    progressDialog.dismiss();
                }
            },700);
        }
        smecAllocOrder=s;
        activityAllocBinding.setAlloc(smecAllocOrder);
        smecAllocAdapter.setArrayList(smecAllocOrder.getAllocMatnrList());
        activityAllocBinding.invalidateAll();
    }


    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_ALLOC_FAILURE)
            }
    )
    public void allocFailure(Object o){
        if(progressDialog != null && progressDialog.isShowing()){
            activityAllocBinding.getRoot().postDelayed(new Runnable() {
                @Override
                public void run() {
                    progressDialog.dismiss();
                }
            },700);
        }
    }
}
